const app = getApp()
import BaseModel from '../../utils/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav_id:3,//导航id
    navScore_id:0,//分数导航id
    start_points:0,//开始分数
    end_points:100,//结束分数
    page:1,
    size:10, //每页条数
    vipOrdersData:[], //待付款数据
    footer_more:"", //无数据
    empty_title:"",
    empty_img:"",
    empty_again:"",//重试
    is_clickTab:0, //重试或下拉是0，上啦是1
    posterData:"",//生成的海报数据
    title_tips:"",//海报文字
    model_type:"",//当前弹窗是早会送1  卖会员0
    roleCode:"",//角色  1 分总  2员工  3财务
    //copy:"",//是否已经复制，1复制 2已复制
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(app.globalData.vip_type)
    this.setData({
      nav_id:app.globalData.vip_type,
      roleCode: wx.getStorageSync('roleCode'),
    })
    if(this.data.nav_id==5){
      this.getExchangeCodeData();
    }else if(this.data.nav_id==6){
      this.getImportantCustomerData();
    }else{
      this.getVipOrdersData();
    }
  },
  /*点击头部导航*/
  clickNav:function(e){
    this.setData({
      nav_id:e.currentTarget.dataset.id,
      is_clickTab:0,
      page:1,
      navScore_id:0,
      start_points:0,
      end_points:100,
      footer_more:"", //无数据
      empty_title:"",
      empty_img:"",
    })
    app.globalData.vip_type = e.currentTarget.dataset.id;
    if(this.data.nav_id==5){
      this.getExchangeCodeData();
    }else if(this.data.nav_id==6){
      this.getImportantCustomerData();
    }else{
      this.getVipOrdersData();
    }
  },
  /*点击分数查询*/
  click_tabScore:function(e){
    this.setData({
      is_clickTab:0,
      page:1,
      navScore_id:e.currentTarget.dataset.id,
      start_points:e.currentTarget.dataset.start_points,
      end_points:e.currentTarget.dataset.end_points,
    })
    this.getImportantCustomerData();
  },
  // 获取重要用户数据
  getImportantCustomerModel:new BaseModel({
    url: "employees/MemberData/listMemberData",
    method:"get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getImportantCustomerData(){
    this.getImportantCustomerModel
        .fetch({
          type_id:1,//导航标识  默认我负责的
          page:this.data.page,
          size:this.data.size,
          fromuid:"",
          chose:"",
          keyword:"",
          start_points:this.data.start_points,//开始分数
          end_points:this.data.end_points,//结束分数
        })
        .then(res => {
          if(this.data.is_clickTab==0){
            this.setData({
              vipOrdersData:res.data,
              empty_title:"暂无数据",
              empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
              empty_again:"",//重试
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"没有更多数据了"
              })
            }else{
              //成功
              var dataList_list = this.data.vipOrdersData;
              for(var i=0;i<res.data.length;i++){
                dataList_list.push(res.data[i])
              }
              this.setData({
                vipOrdersData:dataList_list
              })
            }
          }
        })
        .catch(res => {
          this.setData({
            empty_title:"请求失败",
            empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/empty_data.png",
            empty_again:"重试",//重试
          })
        })
  },

  /*复制兑换码*/
  copy_code:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    var copy = e.currentTarget.dataset.copy;
    var message = that.data.vipOrdersData;
    wx.setClipboardData({
      data: e.currentTarget.dataset.code,
      success (res) {
        wx.getClipboardData({
          success (res) {
            console.log(res.data) // data
            if(copy==1){
              that.getCopyCodeData(id)
              for (var i=0;i<message.length;i++) { //遍历列表数据
                for(var j=0;j<message[i].exchange.length;j++){
                  if(id==message[i].exchange[j].id){
                    message[i].exchange[j].copy = 2
                  }
                }
              }
              that.setData({
                vipOrdersData: message,
              })
            }
          }
        })
      }
    })
  },
  /*下载早会送图片*/
  download_zhsImg:function(){
    this.setData({
      model_type:1
    })
    this.getMorningCodeImgData();
  },
  /*下载卖会员图片*/
  download_vipImg:function(){
    this.setData({
      model_type:0
    })
    this.getDownloadImgData();
  },
  /*获取早会送二维码图片*/
  getMorningCodeImgModel: new BaseModel({
    url: 'employees/NewMember/MorningCode',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getMorningCodeImgData() {
    this.getMorningCodeImgModel
        .fetch({})
        .then(res => {
          //成功
          this.setData({
            posterData:res.data,
            title_tips:"扫码领取7天会员"
          })
          wx.showLoading({
            title: '海报生成中...',
          });
          this.setData({
            isshow: true,
            template: this.palette()
          })
          //成功
        })
        .catch(res => {
          //失败
        })
  },
  /*获取卖会员二维码图片*/
  getDownloadImgModel: new BaseModel({
    url: 'employees/NewMember/memberCode',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getDownloadImgData() {
    this.getDownloadImgModel
        .fetch({})
        .then(res => {
          //成功
          this.setData({
            posterData:res.data.code_img,
            title_tips:"好望角商学院会员"
          })
          wx.showLoading({
            title: '海报生成中...',
          });
          this.setData({
            isshow: true,
            template: this.palette()
          })
          //成功
        })
        .catch(res => {
          //失败
        })
  },
  onImgOK(e) {
    wx.hideLoading();
    this.imagePath = e.detail.path;
  },
  /*点击隐藏海报*/
  share_posterHide:function(){
    this.setData({
      isshow:false,
    })
  },
  /*长按保存图片*/
  longTap: function(e){
    var that =this;
    wx.showModal({
      title: '提示',
      content: '保存分享',
      success(res) {
        if (res.confirm) {
          wx.saveImageToPhotosAlbum({
            filePath: that.imagePath,
            success(res) {
              wx.showToast({
                title: '保存成功',
                icon: 'success',
                duration: 2000
              })
            },
            fail(res) {
              wx.showToast({
                title: '保存失败，请重试',
                icon: 'none',
                duration: 2000
              })
            },
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /*保存图片*/
  saveImage() {
    wx.saveImageToPhotosAlbum({
      filePath: this.imagePath,
      success(res) {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 2000
        })
      },
      fail(res) {
        wx.showToast({
          title: '保存失败，请重试',
          icon: 'none',
          duration: 2000
        })
      },
    });
  },
  /*禁止页面滚动*/
  preventTouchMove:function(e) {

  },
  palette() {
    return ({
      width: '460rpx',
      height: '540rpx',
      background: '',
      views: [{
        type:"image",  //头像
        url: this.data.posterData,
        css:[{
          top:"20rpx",
          left:"20rpx",
          width:"420rpx",
          height:"420rpx",
          borderWidth:0
        }]
      },
        {
          type:"text",
          text: this.data.title_tips,
          css:[{
            align: 'center',
            bottom:"50rpx",
            left:"230rpx",
            fontSize:"40rpx",
            fontWeight: 'bold',
            color:"#000"
          }]
        }
      ],
    });
  },
  /*复制兑换码*/
  getCopyCodeDataModel: new BaseModel({
    url: 'employees/NewMember/copyExchange',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getCopyCodeData(id) {
    this.getCopyCodeDataModel
        .fetch({
          id:id,
        })
        .then(res => {
          //成功
          console.log("复制成功")
        })
        .catch(res => {

        })
  },

  /*获取数据*/
  getVipOrdersDataModel: new BaseModel({
    url: 'employees/NewMember/memberOrder',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getVipOrdersData() {
    this.getVipOrdersDataModel
        .fetch({
          page:this.data.page,
          size:this.data.size, //每页条数
          type:this.data.nav_id,//导航id
        })
        .then(res => {
          //成功
          if(this.data.is_clickTab==0){
            this.setData({
              vipOrdersData:res.data,
              empty_title:"暂无数据",
              empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
              empty_again:"",//重试
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"没有更多数据了"
              })
            }else {
              //成功
              var vipOrdersData_list = this.data.vipOrdersData;
              for(var i=0;i<res.data.length;i++){
                vipOrdersData_list.push(res.data[i])
              }
              this.setData({
                vipOrdersData:vipOrdersData_list
              })
            }
          }

        })
        .catch(res => {
          this.setData({
            empty_title:"请求失败",
            empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/empty_data.png",
            empty_again:"重试",//重试
          })
        })
  },
  /*获取兑换码数据*/
  getExchangeCodeDataModel: new BaseModel({
    url: 'employees/NewMember/ExchangeList',
    method: "get",
    needAjaxLoading:true,
    loadingText:"玩命加载中"
  }),
  getExchangeCodeData() {
    this.getExchangeCodeDataModel
        .fetch({
          page:this.data.page,
          size:this.data.size, //每页条数
        })
        .then(res => {
          //成功
          if(this.data.is_clickTab==0){
            this.setData({
              vipOrdersData:res.data,
              empty_title:"暂无数据",
              empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon_new3.0/empty_mes.png",
              empty_again:"",//重试
            })
          }else{
            if(res.data==""||res.data==null){
              this.setData({
                footer_more:"没有更多数据了"
              })
            }else {
              //成功
              var vipOrdersData_list = this.data.vipOrdersData;
              for(var i=0;i<res.data.length;i++){
                vipOrdersData_list.push(res.data[i])
              }
              this.setData({
                vipOrdersData:vipOrdersData_list
              })
            }
          }

        })
        .catch(res => {
          this.setData({
            empty_title:"请求失败",
            empty_img:"http://hwj2018.oss-cn-beijing.aliyuncs.com/hwj2019/icon/empty_data.png",
            empty_again:"重试",//重试
          })
        })
  },
  /*拨打电话*/
  call_phone:function(e){
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone //仅为示例，并非真实的电话号码
    })
  },
  //重试
  again_request:function(){
    this.setData({
      is_clickTab:0,
      page:1
    })
    if(this.data.nav_id==5){
      this.getExchangeCodeData();
    }else if(this.data.nav_id==6){
      this.getImportantCustomerData();
    }else{
      this.getVipOrdersData();
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      is_clickTab:0,
      page:1
    })
    if(this.data.nav_id==5){
      this.getExchangeCodeData();
    }else if(this.data.nav_id==6){
      this.getImportantCustomerData();
    }else{
      this.getVipOrdersData();
    }
    wx.stopPullDownRefresh(); //停止下拉
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.data.page+=1;
    this.setData({
      is_clickTab:1,
    })
    if(this.data.nav_id==5){
      this.getExchangeCodeData();
    }else if(this.data.nav_id==6){
      this.getImportantCustomerData();
    }else{
      this.getVipOrdersData();
    }

  },

  /**
   * 用户点击右上角分享
   */
  /*onShareAppMessage: function () {

  }*/
})